//
// Created by Jisam on 2024/7/18.
//
//
// Created by Jisam on 2024/7/15.
//
#include "iostream"
#include "vector"
using namespace std;

int n, m;

void solve() {
    cin >> n >> m;

    vector<vector<int>> a(n, vector(m, 0)), b(n, vector(m, 0));

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
        }
    }
    if (n == 1 && m == 1) {
        cout << "-1\n";
        return;
    }

    for (int j = 0; j < m; j++) {
        for (int i = 0; i < n - 1; i++) {
            b[i][j] = a[i + 1][j];
        }
        b[n - 1][j] = a[0][j];
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m-1; j++) {
            a[i][j] = b[i][j + 1];
        }
        a[i][m - 1] = b[i][0];
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cout << a[i][j] << " ";
        }
        cout << "\n";
    }
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;cin >> T;    while (T--)
        solve();
    return 0;
}